/**
 * 
 */
package com.nxn.core.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "t_role")
public class Role extends IdEntity {
	
	private String name;
	private String comments;
	private Set<RoleAuth> roleAuths;
	
	@Column(name = "name", unique = true, nullable = false)
	public String getName() {
		return name;
	}
	@Column(name = "comments")
	public String getComments() {
		return comments;
	}
	@OneToMany(mappedBy = "role")
	@Cascade({CascadeType.MERGE,CascadeType.DELETE_ORPHAN})
	@Fetch(FetchMode.SUBSELECT)
//	@OrderBy("resource.id, action.id")
	@OrderBy("resource, action")
	public Set<RoleAuth> getRoleAuths() {
		return roleAuths;
	}

	public void setName(String name) {
		this.name = name;
	}
	public void setComments(String comments) {
		this.comments = comments;
	}
	public void setRoleAuths(Set<RoleAuth> roleAuths) {
		this.roleAuths = roleAuths;
	}
	
	public void addRoleAuths(RoleAuth permission) {
		if(roleAuths == null) {
			roleAuths = new HashSet<RoleAuth>();
		}
		
		roleAuths.add(permission);
	}
}